<template>
  <div class="production">
    <div class="btns">
      <!-- 检索框 -->
      <el-form :inline="true" :model="params" size="small">
        <el-form-item>
          <el-select clearable v-model="params.status" placeholder="请选择状态">
            <el-option label="未审核" value="未审核"></el-option>
            <el-option label="审核通过" value="审核通过"></el-option>
            <el-option label="审核不通过" value="审核不通过"></el-option>
            <el-option label="推荐" value="推荐"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="params.name" placeholder="标题"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="params.id"
            placeholder="请选择栏目"
            clearable
          >
            <el-option
              v-for="(item,index) of categories"
              :label="item.name"
              :value="item.id"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button
          type="primary"
          size="small"
          style="margin-right: 10px"
          @click="pageQueryProductions"
          icon="el-icon-search"
          >查询</el-button
        >
      </el-form>
    </div>
    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData.list" style="width: 100%" size="small">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="图片" prop="">
          <template v-slot="scope">
            <img :src="scope.row.cover" width="150" alt="图片丢失" />
          </template>
        </el-table-column>
        <el-table-column label="风采" >
          <template v-slot="scope">
            <h3>{{scope.row.title}}</h3>
            <!-- <span>{{scope.row.baseUser.realname}}</span><br> -->
            <span>{{scope.row.category.name}}</span><br>
            <span>{{scope.row.publishTime | fmtDate}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.status == '审核通过'"
              size="mini"
              type="success"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag 
              v-else-if="scope.row.status == '审核不通过'" 
              size="mini" 
              type="danger"
            >
              {{ scope.row.status }}
            </el-tag>
            <el-tag 
              v-else-if="scope.row.status == '未审核'" 
              size="mini" 
              type="info"
            >
              {{ scope.row.status }}
            </el-tag>
            <el-tag 
              v-else-if="scope.row.status == '推荐'" 
              size="mini" 
              type="warning"
            >
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="changeHandler(scope.row.id,scope.row.status)" style="color: #2cb5ad">推荐</el-button>
            <el-button size="small" type="text" @click="checkHandler(scope.row.id)" style="color: #3a71a8">审核</el-button>
            <el-button type="text" size="small" @click="delHandler(scope.row.id)" style="color: #e65e6f">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- total表示：数据的总条数  current-page表示：当前所在的页数   -->
      <el-pagination
        background
        layout="prev, pager, next, sizes"
        :total="resData.total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="pageChangeHandler"
        :page-sizes="[5, 10, 15]"
        @size-change="pageSizeChangeHandler"
      >
      </el-pagination>
      <!-- 模态框 -->
      <el-dialog :title="title" :visible.sync="centerVisible" >
        <el-form  :model="production" >
          <el-form-item>
            {{production.content}}
          </el-form-item >
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="centerVisible = false">取 消</el-button>
          <el-button type="primary" @click="toSaveHandler">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
import { mapActions } from "vuex";
import qs from "qs";
import _ from "lodash";


export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
      },
      resData: {},
      categories: [],

      title: "",
      production: {},
      centerVisible:false,
      formLabelWidth: '80px',

    };
  },
  methods: {
    // 导入状态机中的方法
    ...mapActions("source", ["findAllSources"]),
    // 分页查询产品表格
    async pageQueryProductions() {
      // 过滤数据将空字符串删除
      for (let key in this.params) {
        if (this.params[key] == "") {
          delete this.params[key];
        }
      }
      let res = await request.get("/article/pageQuery", {
        params: this.params,
      });
      this.resData = res.data;
    },
    // page改变事件  
    pageChangeHandler(page) {
      // 将更新了的页码传递给页面   并且刷新查询到的对应页码上的内容
      this.params.page = page;
      this.pageQueryProductions();
    },
    // pageSize改变事件  pageSize是每页中显示的数据条目
    pageSizeChangeHandler(pageSize) {
      this.params.pageSize = pageSize;
      this.pageQueryProductions();
    },

    // 添加完成后的保存事件
    toSaveHandler(){
      // this.$refs["saveForm"].validate((valid) => {
      //   if(valid) {
      //     request.post("/article/saveOrUpdate", qs.stringify(this.production))
      //       .then((res) => {
      //         this.pageQueryProductions(); // 重载数据
      //         this.$message.success(res.message); // 提示操作结果信息
      //         this.centerVisible = false; // 关闭模态框
      //       });
      //   } else {
      //     return fasle;
      //   }
      // });
    },


    // 关闭模态框的回调函数
    dialogCloseHandler() {
      // 置空表单验证
      this.$refs["saveForm"].resetFileds();
    },

    // 推荐  事件
    changeHandler(id,status){
      if(status != '推荐'){
        request.post('/article/changeStatus', { id,status }).then((res) => {
          this.pageQueryProductions(); // 重载数据
          this.$message.success(res.message); // 提示操作结果信息
        })
      }
    },

    // 审核  事件
    checkHandler(){
      this.title = "审核信息";
      this.centerVisible = true;
    },

    // 删除 事件
    delHandler(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          request.get('/article/deleteById',{ params:{id} }).then((res) => {
              // 刷新数据
              this.pageQueryProductions();
              this.$message({
                  type: "success",
                  message: res.message,
              }); // 提示成功信息
          })
      })
    },
  },
  created() {
    this.pageQueryProductions(); // 分页查询产品
    this.findAllSources().then((res) => {
      this.categories = res.data;
    }); // 查询所有产品栏目
  },
};
</script>

<style scoped>

</style>